package com.kt.uokos.yimo.service.impl;

import com.github.pagehelper.PageHelper;
import com.kt.uokos.yimo.dao.Topic;
import com.kt.uokos.yimo.example.TopicExample;
import com.kt.uokos.yimo.mapper.TopicMapper;
import com.kt.uokos.yimo.service.TopicService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;



@Service
public class TopicServiceImpl implements TopicService {

    @Resource
    private TopicMapper topicMapper;


    @Override
    public List<Topic> queryList(String title, String subtitle, int page, int limit, String sort, String order) {
        TopicExample example = new TopicExample();
        TopicExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(title)){
            criteria.andTitleLike("%"+title+"%");
        }
        if (!StringUtils.isEmpty(subtitle)){
            criteria.andSubtitleLike("%"+subtitle+"%");
        }
        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            example.setOrderByClause(sort+" "+order);
        }
        PageHelper.startPage(page,limit);
        return topicMapper.selectByExample(example);
    }

    @Override
    public List<Topic> query(Integer offset, Integer limit, String sort, String order) {
        TopicExample example = new TopicExample();
        TopicExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        return topicMapper.selectByExample(example);
    }

    @Override
    public Topic findById(Integer id) {
        return topicMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Topic topic) {
        topic.setAddTime(LocalDateTime.now());
        topic.setUpdateTime(LocalDateTime.now());
        topicMapper.insertSelective(topic);
    }

    @Override
    public void delete(Topic topic) {
        topicMapper.deleteByPrimaryKey(topic.getId());
    }

    @Override
    public void update(Topic topic) {
        topic.setUpdateTime(LocalDateTime.now());
        topicMapper.updateByPrimaryKeySelective(topic);
    }

    @Override
    public Topic queryById(Integer id) {
        return topicMapper.selectByPrimaryKey(id);
    }
}
